package cn.hbu.zlfw.admin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hbu.zlfw.admin.entity.Expert;
import cn.hbu.zlfw.admin.entity.RootAdmin;
import cn.hbu.zlfw.admin.entity.ScienceExpert;
import cn.hbu.zlfw.admin.entity.UnitAdmin;
import cn.hbu.zlfw.admin.service.ExpertService;
import cn.hbu.zlfw.admin.service.RootAdminService;
import cn.hbu.zlfw.admin.service.ScienceExpertService;
import cn.hbu.zlfw.admin.service.UnitAdminService;
import cn.hbu.zlfw.annotation.OperatorLog;
import cn.hbu.zlfw.annotation.OperatorType;
import cn.hbu.zlfw.common.util.SessionUtil;
import cn.hbu.zlfw.dic.entity.DicUserType;
import cn.hbu.zlfw.dic.service.DicUserTypeService;
import cn.hbu.zlfw.research.entity.Researcher;
import cn.hbu.zlfw.research.service.ResearcherService;

/**
 * 
 * @ClassName: LoginController 
 * @Description: 后台登录控制器
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年11月27日 下午11:27:37 
 * @vsersion:v1.0
 */
@Controller
@RequestMapping("/admin")
public class LoginController {
	/**
	 * 日志
	 */
	public final Logger logger=LoggerFactory.getLogger(LoginController.class);
	/**
	 * 注入HttpServletRequest
	 */
	@Autowired
	private HttpServletRequest request;
	/**
	 * 注入service
	 */
	@Autowired
	private DicUserTypeService dicUserTypeService;
	@Autowired
	private UnitAdminService unitAdminService;
	@Autowired
	private ScienceExpertService scienceExpertService;
	@Autowired
	private RootAdminService rootAdminService;
	@Autowired
	private ResearcherService researcherService;
	@Autowired 
	ExpertService expertService;
	/**
	  * @Title: login 
	  * @Description: 进行登录操作
	  * @return String 
	  * @throws
	 */
	@ResponseBody
	@OperatorLog(type=OperatorType.OTHER,description="登录成功,转向后台管理界面")
	@RequestMapping("/login.html")
	public String login(@RequestParam(value="phoneNumber",required=false)String phoneNumber,@RequestParam("password")String password,
					@RequestParam(value="userType",required=false)Integer userType) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("password", password);
		if(DicUserType.ROOT_ADMIN_USER.equals(userType)) {	//总管理员登录
			params.put("userName", phoneNumber);
			RootAdmin rootAdmin = this.rootAdminService.getRootAdminByParam(params);
			if(rootAdmin == null) {
				return "fail";
			} else {
				request.getSession().setAttribute(SessionUtil.SESSION_USER_KEY, rootAdmin);
				return DicUserType.ROOT_ADMIN_USER.toString();
			}
		} else if (DicUserType.UNIT_ADMIN_USER.equals(userType)) {	//单位管理员登录
			params.put("phoneNumber", phoneNumber);
			UnitAdmin unitAdmin = this.unitAdminService.getUnitAdminByParam(params);
			if(unitAdmin == null) {
				return "fail";
			} else {
				request.getSession().setAttribute(SessionUtil.SESSION_USER_KEY, unitAdmin);
				return DicUserType.UNIT_ADMIN_USER.toString();
			}
		} else if (DicUserType.SCIENCE_SERVICE_USER.equals(userType)){	//科技服务人员
			params.put("phoneNumber", phoneNumber);
			ScienceExpert scienceExpert = this.scienceExpertService.getScienceExpertByParam(params);
			if(scienceExpert == null) {
				return "fail";
			} else {
				request.getSession().setAttribute(SessionUtil.SESSION_USER_KEY, scienceExpert);
				return DicUserType.SCIENCE_SERVICE_USER.toString();
			}
		}else if (DicUserType.RESEARCHER_USER.equals(userType)){	//科研人员
			Researcher researcher=this.researcherService.
					findResearcherTologin(phoneNumber,password);
			if(researcher==null) {	//密码不匹配登录失败
				return "fail";
			} else {			//登录成功进行页面跳转
				this.request.getSession().setAttribute(SessionUtil.SESSION_USER_KEY, researcher);
				return DicUserType.RESEARCHER_USER.toString();
			}
		} else if (DicUserType.EXPERT_SERVICE_USER.equals(userType)){	//专家
			params.put("phoneNumber", phoneNumber);
			Expert expert = this.expertService.getExpertByParam(params);
			if(expert == null) {
				return "fail";
			} else {
				request.getSession().setAttribute(SessionUtil.SESSION_USER_KEY, expert);
				return DicUserType.EXPERT_SERVICE_USER.toString();
			}
		} 
		else {
			return "error";
		}
	}
	/**
	  * 
	  * @Title: loginUI 
	  * @Description: 跳转到登录界面
	  * @return String 
	  * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="管理管理员进行登录")
	@RequestMapping("/loginUI.html")
	public String loginUI(ModelMap map,Integer userType) {
		List<DicUserType> tempdicUserTypes = dicUserTypeService.findAll();
		List<DicUserType> dicUserTypes = new ArrayList<DicUserType>();
		for (DicUserType dicUserType : tempdicUserTypes) {
			//if(!dicUserType.getTypeCode().equals(DicUserType.RESEARCHER_USER)) {
				dicUserTypes.add(dicUserType);
			//}
		}
		map.put("dicUserTypes", dicUserTypes);
		map.put("userType", userType);
		return "admin/login";
	}
	/**
	  * 分发请求
	  * @Title: redirect 
	  * @Description: 
	  * @return String 
	  * @throws
	 */
	@RequestMapping("/redirect.do")
	public String redirect(@RequestParam(value="userType",required=false)Integer userType) {
		if(DicUserType.ROOT_ADMIN_USER.equals(userType)) {
			return "forward:/rootAdmin/tomain.do";
		} else if (DicUserType.UNIT_ADMIN_USER.equals(userType)) {
			return "forward:/unitAdmin/tomain.do";
		} else if(DicUserType.SCIENCE_SERVICE_USER.equals(userType)){
			return "forward:/scienceExpert/tomain.do";
		} else if(DicUserType.RESEARCHER_USER.equals(userType)){
			return "forward:/patent/topatentlist.action";
		}  else if(DicUserType.EXPERT_SERVICE_USER.equals(userType)){
			return "forward:/expert/tomain.do";
		}else {
			return "admin/main";
		}
	}
	/**
	  * @Title: totop 
	  * @Description: 加载管理页面底部
	  * @throws
	 */
	@RequestMapping("/top.do")
	public String totop() {
		return "admin/top";
	}
	/**
	  * @Title: toleft 
	  * @Description: 加载管理界面左侧
	  * @return String 
	  * @throws
	 */
	@RequestMapping("/left.do")
	public String toleft() {
		return "admin/left";
	}
	/**
	  * @Title: toindex 
	  * @Description: 加载注管理界面
	  * @return String 
	  * @throws
	 */
	@RequestMapping("/index.do")
	public String toindex() {
		return "admin/index";
	}
	/**
	  * @Title: toPatentList 
	  * @Description: 加载专利列表
	  * @return String 
	  * @throws
	 */
	@RequestMapping("/patentlist.do")
	public String toPatentList() {
		return "admin/patentlist";
	}
	/**
	 * 	
	  * @Title: toUserList 
	  * @Description: 加载用户列表
	  * @return String 
	  * @throws
	 */
	@RequestMapping("/userlist.do")
	public String toUserList() {
		return "admin/userlist";
	}
	/**
	  * @Title: toPatentList 
	  * @Description: 加载全部专利列表
	  * @return String 
	  * @throws
	 */
	@RequestMapping("/allpatentlist.do")
	public String allPatentList(@RequestParam(value="userType",required=false)Integer userType) {
		if(DicUserType.ROOT_ADMIN_USER.equals(userType)) {
			 return null;
		} else if (DicUserType.UNIT_ADMIN_USER.equals(userType)){
			 return null;
		} else if (DicUserType.SCIENCE_SERVICE_USER.equals(userType)) {
			 return "forward:/scienceExpert/listPatentByParam.do"; 
		} else {
			return "admin/main";
		}
	}
	

}
